/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package lichbaogiang;

import java.sql.*;
import java.net.*;
import javax.swing.*;

/**
 *
 * @author Administrator
 */
public class InsertSubject extends JPanel {

    
    int stt = 0, tc, lt, th, da, tt;
    String tenHP = "", lop = "", maHP = "";
    static int checkAdd = 0;
    static String sql = "";
    /**
     * Creates new form InsertSubject
     */
    public InsertSubject() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbLHP = new javax.swing.JLabel();
        txtLHP = new javax.swing.JTextField();
        lbMLHP = new javax.swing.JLabel();
        txtMLHP = new javax.swing.JTextField();
        lbLop = new javax.swing.JLabel();
        txtLop = new javax.swing.JTextField();
        cbTinChi = new javax.swing.JComboBox();
        txtTinChi = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        lbDSLHP = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(800, 110));

        lbLHP.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbLHP.setText("Tên Lớp HP");

        txtLHP.setBackground(new java.awt.Color(255, 255, 255));

        lbMLHP.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbMLHP.setText("Mã Lớp HP");

        txtMLHP.setBackground(new java.awt.Color(255, 255, 255));

        lbLop.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbLop.setText("Lớp");

        txtLop.setBackground(new java.awt.Color(255, 255, 255));

        cbTinChi.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        cbTinChi.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "LT", "TH", "DA", "TT" }));

        txtTinChi.setBackground(new java.awt.Color(255, 255, 255));

        btnAdd.setBackground(new java.awt.Color(0, 153, 51));
        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnAdd.setText("Thêm");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        lbDSLHP.setBackground(new java.awt.Color(0, 102, 0));
        lbDSLHP.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbDSLHP.setForeground(new java.awt.Color(0, 102, 0));
        lbDSLHP.setText("DANH SÁCH LỚP HỌC PHẦN");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbLHP)
                            .addComponent(lbLop))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtLHP)
                            .addComponent(txtLop, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                        .addGap(88, 88, 88)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbMLHP)
                            .addComponent(cbTinChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbDSLHP)
                        .addGap(17, 17, 17)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMLHP, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtTinChi, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAdd)))
                .addContainerGap(71, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbLHP)
                    .addComponent(txtLHP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbMLHP)
                    .addComponent(txtMLHP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbLop)
                    .addComponent(txtLop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbTinChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTinChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAdd))
                .addGap(18, 18, 18)
                .addComponent(lbDSLHP)
                .addGap(32, 32, 32))
        );

        lbLHP.getAccessibleContext().setAccessibleName("lbLHP");
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        
        stt = ++ MonHoc.count_row;
        System.out.println(stt);
        int indexcb = cbTinChi.getSelectedIndex();
        switch(indexcb)
        {
            case 0:
                lt = Integer.parseInt(txtTinChi.getText());
                th = da = tt = 0;
                break;
            case 1:
                th = Integer.parseInt(txtTinChi.getText());
                lt = da = tt = 0;
                break;
            case 2:
                da = Integer.parseInt(txtTinChi.getText());
                th = lt = tt = 0;
                break;
            case 3:
                tt = Integer.parseInt(txtTinChi.getText());
                th = da = lt = 0;
                break;
        }
        tc = lt + th + da + tt;
        lop = txtLop.getText();
        maHP = txtMLHP.getText();
        tenHP = txtLHP.getText();
        if(JOptionPane.showConfirmDialog(this,"Bạn có chắc chắn thêm?","Yes/No!",JOptionPane.YES_NO_OPTION) == 0) 
        {
            sql = "insert into DSPC values('"+stt+"','"+tenHP+"','"+tc+"','"+lt+"','"+th+"','"+da+"','"+tt+"','"+lop+"','"+maHP+"')";
            //sql = "insert into DSPC values('139','ToanRR','2','2','0','0','0','101332NHT','21213246454')";
            try{
                String url = "jdbc:odbc:CNPM";
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection conn = DriverManager.getConnection(url);
                Statement stmt = conn.createStatement();
                stmt.execute(sql);
            }
            catch(Exception e){}
            JOptionPane.showConfirmDialog(this, "Thêm thành công, Nhấn Cập Nhật để xem!", "Thành công!", JOptionPane.DEFAULT_OPTION);
        }
    }//GEN-LAST:event_btnAddActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JComboBox cbTinChi;
    private javax.swing.JLabel lbDSLHP;
    private javax.swing.JLabel lbLHP;
    private javax.swing.JLabel lbLop;
    private javax.swing.JLabel lbMLHP;
    private javax.swing.JTextField txtLHP;
    private javax.swing.JTextField txtLop;
    private javax.swing.JTextField txtMLHP;
    private javax.swing.JTextField txtTinChi;
    // End of variables declaration//GEN-END:variables
}
